package com.tsp_tux.demo.Controller;

import com.tsp_tux.demo.Entity.RespBean;
import com.tsp_tux.demo.Entity.User;
import com.tsp_tux.demo.Service.UserService;
import com.tsp_tux.demo.Util.easyRespBean;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 通过用户名查询用户
 *
 * @author 张帅
 */
@RestController
@CrossOrigin
public class SearchController {
    @Resource
    private UserService userService;

    @PostMapping("/SearchUserName")
    @ResponseStatus(HttpStatus.CREATED)
    @CrossOrigin
    public RespBean SearchUserName(@RequestBody Map map) {
        User user1 = userService.SearchUserName((String) map.get("username"));
        if (user1 == null) {
            return easyRespBean.noFound();
        }
        return easyRespBean.ok(user1);
    }

    @PostMapping("/SearchUserNamemohu")
    @CrossOrigin
    public RespBean SearchUserNamemohu(@RequestBody Map map) {

        List<User> user2 = userService.SearchUserNamemohu((String) map.get("username"));
        if (user2.isEmpty()) {
            return easyRespBean.noFound();
        }
        return easyRespBean.ok(user2);
    }

    /**
     * @return 返回用户信息
     * @author 童一凡
     */
    @RequestMapping("/SearchUserID")
    @ResponseBody
    public RespBean SearchUserID(HttpSession session, HttpServletRequest request) {
        String uid = request.getParameter("uid");
        int uid_length = uid.length();
        List<User> uid_List = userService.SearchUserID(uid);
        if (uid_length == 12) {
            if (uid_List.isEmpty()) {
                return easyRespBean.noFound();
            }
            return easyRespBean.ok(uid_List);
        }
        return new RespBean(403, "ID格式错误", null);
    }
}
